﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Net.Mail;

namespace Get_IP
{
    class Program
    {
        static void Main(string[] args)
        {
            Program pro = new Program();
            string content = pro.check("http://1111.ip138.com/ic.asp");
            string con = pro.REGEX(content);
            try
            {
                if (args[0] == "-C")
                {
                    pro.write(con);
                }
                if (args[0] == "-M")
                {
                    pro.sendMessage(con);
                }
                if (args[0] == "-O")
                {
                    pro.write(con);
                    pro.sendMessage(con);
                }
            }
            catch(IndexOutOfRangeException)
            {
                pro.write(con);
                Thread th = new Thread(delegate()
                {
                    pro.clipboard(con);
                });
                th.SetApartmentState(ApartmentState.STA);
                th.Start();
            }
        }

        public string check(string url)
        {
            WebClient wc = new WebClient();
            byte[] data = wc.DownloadData(url);
            string content = Encoding.GetEncoding("GB2312").GetString(data);
            //Console.WriteLine(content);
            return content;
        }
        public string REGEX(string con)
        {
            Regex re = new Regex(@"(?<=\[)(.+?)(?=\])");
            Match ma = re.Match(con);
            string res = ma.ToString();
            //Console.WriteLine(DateTime.Now.ToString()+"-->"+res);
            
            return res;
        }
        public void write(string con)
        {
            FileStream file = new FileStream("D:\\fNGF\\IP.txt", FileMode.Append);
            con = DateTime.Now.ToString() + "-->" + con + "\r\n";
            byte[] by_con = Encoding.Default.GetBytes(con);
            file.Write(by_con,0,by_con.Length);
            file.Close();
        }
        public void clipboard(string text)
        {

            Clipboard.SetData(DataFormats.Text, text);
        }
        public void sendMessage(string IP)
        {
            MailMessage mail = new MailMessage();
            //设置发件人
            mail.From = new MailAddress("Youremail@163.com");
            mail.To.Add("receiveEmail@163.com");
            mail.Subject = DateTime.Now.ToString() + " --> " + IP;
            mail.BodyEncoding = Encoding.Default;
            mail.Priority = MailPriority.High;
            mail.Body = DateTime.Now.ToString() + " --> " + IP;
            SmtpClient smtp = new SmtpClient("smtp.163.com");
            smtp.UseDefaultCredentials = true;
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            //设置发件人账号信息
            smtp.Credentials = new System.Net.NetworkCredential("Youremail@163.com", "password");
            smtp.Timeout = 10000;
            smtp.Send(mail);
            Console.WriteLine("fasongwanbi");
        }
    }
}
